//
//  BIDMemberListForReceiveConditionViewController.m
//  zhangqitong
//
//  Created by mal on 15/12/18.
//  Copyright © 2015年 mal. All rights reserved.
//

#import "BIDReceiveConditionForOutingReportViewController.h"
#import "BIDOutingReportConditionCell.h"
/**
 *  接收情况
 */
static NSString *receiveConditionURL = @"outingAndroid!listReceiveState.action";

@interface BIDReceiveConditionForOutingReportViewController ()<BIDBaseListViewDelegate>
{
    BIDOutingReportConditionCell *_temporaryCell;
    BOOL _bRegister;
}
@end

@implementation BIDReceiveConditionForOutingReportViewController
@synthesize readedArr;
@synthesize unReadArr;
@synthesize outingReportId;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"接收情况";
    // Do any additional setup after loading the view.
    _temporaryCell = [[[NSBundle mainBundle] loadNibNamed:@"BIDOutingReportConditionCell" owner:self options:nil] lastObject];
    self.myTableView.allowsSelection = NO;
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], receiveConditionURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"outingReply.outingId=%@", outingReportId];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            readedArr = [[NSArray alloc] initWithArray:responseDic[@"isread"]];
            unReadArr = [[NSArray alloc] initWithArray:responseDic[@"unread"]];
            [self.dataSourceArr addObject:@[]];
            [self.dataSourceArr addObject:@[]];
            [self.myTableView reloadData];
        }
    }];
}

- (void)tapGestureHandler:(UITapGestureRecognizer*)tapGR
{
    UIView *tapView = tapGR.view;
    NSInteger index = tapView.tag;
    NSArray *arr = self.dataSourceArr[index];
    if(arr.count==0)
    {
        if(index==0)
        {
            [self.dataSourceArr replaceObjectAtIndex:index withObject:unReadArr];
        }
        else
        {
            [self.dataSourceArr replaceObjectAtIndex:index withObject:readedArr];
        }
    }
    else
    {
        [self.dataSourceArr replaceObjectAtIndex:index withObject:@[]];
    }
    [self.myTableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSourceArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = self.dataSourceArr[section];
    return arr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *isReadIdentifier = @"identifier_read";
    static NSString *unReadIdentifier = @"identifier_unread";
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDConditionCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:isReadIdentifier];
    }
    NSUInteger section = indexPath.section;
    NSUInteger row = indexPath.row;
    NSArray *arr = self.dataSourceArr[section];
    NSDictionary *dic = arr[row];
    NSString *strName = [[NSString alloc] initWithFormat:@"%@", dic[@"receiveUserName"]];
    if(section == 0)
    {
        //未读
        cell = [tableView dequeueReusableCellWithIdentifier:unReadIdentifier];
        if(!cell)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unReadIdentifier];
        }
        cell.textLabel.text = strName;
        cell.textLabel.font = [UIFont systemFontOfSize:15.f];
        cell.textLabel.textColor = RGBACOLOR(137.f, 164.f, 75.f, 1.f);
    }
    else
    {
        //已读
        BIDOutingReportConditionCell *conditionCell = [tableView dequeueReusableCellWithIdentifier:isReadIdentifier];
        conditionCell.titleLabel.text = strName;
        conditionCell.dateLabel.text = [[NSString alloc] initWithFormat:@"时间:%@", dic[@"readTime"]];
        conditionCell.replyLabel.text = [[NSString alloc] initWithFormat:@"回复:%@", dic[@"content"]];
        cell = conditionCell;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0.f;
    NSUInteger section = indexPath.section;
    NSUInteger row = indexPath.row;
    if(section==1)
    {
        NSArray *arr = self.dataSourceArr[section];
        NSDictionary *dic = arr[row];
        _temporaryCell.replyLabel.preferredMaxLayoutWidth = CGRectGetWidth(tableView.frame)-14-8;
        _temporaryCell.replyLabel.text = [[NSString alloc] initWithFormat:@"回复:%@", dic[@"content"]];
        CGSize size = [_temporaryCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        height = size.height+1;
    }
    else
    {
        height = 44.f;
    }
    return height;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = nil;
    headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 60.f)];
    headerView.userInteractionEnabled = YES;
    [headerView setBackgroundColor:[UIColor clearColor]];
    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, CGRectGetWidth(headerView.frame), 50.f)];
    [subView setBackgroundColor:[UIColor whiteColor]];
    [headerView addSubview:subView];
    //
    UILabel *flagLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 7, 36, 36)];
    [flagLabel setTextAlignment:NSTextAlignmentCenter];
    NSString *strTitle = @"";
    if(section==0)
    {
        strTitle = [[NSString alloc] initWithFormat:@"未读人员(%ld)", unReadArr.count];
    }
    else
    {
        strTitle = [[NSString alloc] initWithFormat:@"已读人员(%ld)", readedArr.count];
    }
    NSString *strFlag = section==0?@"未":@"已";
    UIColor *color = section==0?RGBACOLOR(222.f, 122.f, 61.f, 1.f):RGBACOLOR(79.f, 158.f, 242.f, 1.f);
    [flagLabel setText:strFlag];
    [flagLabel setBackgroundColor:color];
    [flagLabel setFont:[UIFont systemFontOfSize:20.f]];
    [flagLabel setTextColor:[UIColor whiteColor]];
    flagLabel.layer.cornerRadius = CGRectGetHeight(flagLabel.frame)/2;
    flagLabel.clipsToBounds = YES;
    [subView addSubview:flagLabel];
    //
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(52, 0, 150, CGRectGetHeight(subView.frame))];
    [titleLabel setTextColor:[UIColor blackColor]];
    [titleLabel setFont:[UIFont systemFontOfSize:17.f]];
    [titleLabel setText:strTitle];
    //
    [subView addSubview:titleLabel];
    //
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(subView.frame)-0.5f, CGRectGetWidth(subView.frame), 0.5f)];
    [lineLabel setBackgroundColor:RGBACOLOR(179.f, 179.f, 179.f, 1.f)];
    [subView addSubview:lineLabel];
    //
    headerView.tag = section;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
    [headerView addGestureRecognizer:tapGR];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSArray *arr = section==0?unReadArr:readedArr;
    //return arr.count>0?60.f:0.f;
    return 60.f;
}


@end
